﻿using Mapster;
using Meiam.System.Hostd.Authorization;
using Meiam.System.Hostd.Extensions;
using Meiam.System.Interfaces;
using Meiam.System.Model;
using Meiam.System.Model.Dto;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Meiam.System.Hostd.Controllers.Basic
{
    /// <summary>
    /// 接口中心
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ToERPController : BaseController
    {
        /// <summary>
        /// 日志管理接口
        /// </summary>
        private readonly ILogger<ToOracleController> _logger;
        /// <summary>
        /// 会话管理接口
        /// </summary>
        private readonly TokenManager _tokenManager;

        /// <summary>
        /// 业务定义接口
        /// </summary>
        private readonly IBaseToERPService _ToERP;

        /// <summary>
        /// 数据关系接口
        /// </summary>
        private readonly ISysDataRelationService  _dataRelationService;


        public ToERPController(ILogger<ToOracleController> logger, TokenManager tokenManager, IBaseToERPService toERPService, ISysDataRelationService dataRelationService)
        {
            _logger = logger;
            _tokenManager = tokenManager;
            _ToERP = toERPService;
            _dataRelationService = dataRelationService;
        }

        /// <summary>
        /// 工单完工入库接口
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorization(Power = "PRIV_ERP_ALL")]
        public IActionResult CompleteTOOracleERP([FromBody] CompleteRequestDto strparm)
        {
            StringBuilder strMsg = new StringBuilder();
            CompleteResponseDto result = new CompleteResponseDto();
            string statuscode = string.Empty;
            try
            {
                result = _ToERP.CompleteToERP(strparm, strMsg);
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            //return toResponse(StatusCodeType.Success, result+ strMsg.ToString());
            return toResponse<CompleteResponseDto>(StatusCodeType.Success, strMsg.ToString(), result);
        }

        /// <summary>
        /// 搬运单产成品入库
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorization(Power = "PRIV_ERP_ALL")]
        public IActionResult ProductTOERP([FromBody] ProductRequestDto strparm)
        {
            StringBuilder strMsg = new StringBuilder();
            ProductResponseDto result = new ProductResponseDto();
            string statuscode = string.Empty;
            try
            {
                result = _ToERP.ProductTOERP(strparm, out statuscode, strMsg);
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            return toResponse<ProductResponseDto>(StatusCodeType.Success, strMsg.ToString(), result);
            //return toResponse(StatusCodeType.Success, result + strMsg.ToString());
        }

        /// <summary>
        /// 搬运单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        //[Authorization(Power = "PRIV_ERP_MOVE")]
        [Authorization(Power = "PRIV_ERP_ALL")]
        public IActionResult MoveOrderTOERP([FromBody] MoveOrderRequestDto strparm)
        {
            StringBuilder strMsg = new StringBuilder();
            MoveOrderResponseDto result = new MoveOrderResponseDto();
            string statuscode = string.Empty;
            try
            {
                result = _ToERP.MoveOrderTOERP(strparm, out statuscode, strMsg);
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            return toResponse<MoveOrderResponseDto>(StatusCodeType.Success, strMsg.ToString(), result);
            //return toResponse(StatusCodeType.Success, result + strMsg.ToString());
        }

        /// <summary>
        /// 搬运单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        //[Authorization(Power = "PRIV_ERP_MOVE")]
        [Authorization(Power = "PRIV_OA_ALL")]
        public IActionResult PAYMENTTOERP([FromBody] payMentRequestDto strparm)
        {
            StringBuilder strMsg = new StringBuilder();
            payMentResponseDto result = new payMentResponseDto();
            string statuscode = string.Empty;
            try
            {
                result = _ToERP.PayMentTOERP(strparm, out statuscode, strMsg);
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            return toResponse<payMentResponseDto>(StatusCodeType.Success, strMsg.ToString(), result);
            //return toResponse(StatusCodeType.Success, result + strMsg.ToString());
        }

        /// <summary>
        /// 采购及RMA接收
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        //[Authorization(Power = "PRIV_ERP_RECEIVE")]
        [Authorization(Power = "PRIV_ERP_ALL")]
        public IActionResult ReceivingTOERP([FromBody] ReceivingRequestDto strparm)
        {
            StringBuilder strMsg = new StringBuilder();
            ReceivingResponseDto result = new ReceivingResponseDto();
            string statuscode = string.Empty;
            try
            {
                result = _ToERP.ReceivingTOERP(strparm, out statuscode, strMsg);
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            return toResponse<ReceivingResponseDto>(StatusCodeType.Success, strMsg.ToString(), result);
            //return toResponse(StatusCodeType.Success, result + strMsg.ToString());
        }

        /// <summary>
        /// 订单挑库
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        //[Authorization(Power = "PRIV_ERP_PICK")]
        [Authorization(Power = "PRIV_ERP_ALL")]
        public IActionResult PickTOERP([FromBody] PickRequestDto strparm)
        {
            StringBuilder strMsg = new StringBuilder();
            PickResponseDto result = new PickResponseDto();
            string statuscode = string.Empty;
            try
            {
                result = _ToERP.PickTOERP(strparm, out statuscode, strMsg);
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            return toResponse<PickResponseDto>(StatusCodeType.Success, strMsg.ToString(), result);
            //return toResponse(StatusCodeType.Success, result + strMsg.ToString());
        }

        /// <summary>
        /// 库存处理
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        //[Authorization(Power = "PRIV_ERP_TRANSACTIONS")]
        [Authorization(Power = "PRIV_ERP_ALL")]
        public IActionResult TransActionsTOERP([FromBody] TranActionsRequestDto strparm)
        {
            StringBuilder strMsg = new StringBuilder();
            TranActionsResponseDto result = new TranActionsResponseDto();
            string statuscode = string.Empty;
            try
            {
                result = _ToERP.TransActionsTOERP(strparm, out statuscode, strMsg);
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            return toResponse<TranActionsResponseDto>(StatusCodeType.Success, strMsg.ToString(), result);
            //return toResponse(StatusCodeType.Success, result + strMsg.ToString());
        }

        /// <summary>
        /// 工单发料同步ERP接口
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorization(Power = "PRIV_ERP_ALL")]
        public IActionResult FeedingTOOracleERP([FromBody] FeedRequestDto stringjson)
        {
            StringBuilder strMsg = new StringBuilder();
            //string result = string.Empty;
            FeedResponseDto result = new FeedResponseDto();
            string statuscode = string.Empty;
            try
            {
                result = _ToERP.FeedingToERP(stringjson, strMsg);
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            return toResponse<FeedResponseDto>(StatusCodeType.Success, strMsg.ToString(), result);
            //return toResponse(StatusCodeType.Success, result + strMsg.ToString());
        }

        /// <summary>
        /// 工单发料同步ERP接口自动分配批次
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorization(Power = "PRIV_ERP_ALL")]
        public IActionResult NewFeedingTOOracleERP([FromBody] FeedRequestDto stringjson)
        {
            StringBuilder strMsg = new StringBuilder();
            //string result = string.Empty;
            FeedResponseDto result = new FeedResponseDto();
            string statuscode = string.Empty;
            try
            {
                result = _ToERP.NewFeedingToERP(stringjson, strMsg);
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            return toResponse<FeedResponseDto>(StatusCodeType.Success, strMsg.ToString(), result);
            //return toResponse(StatusCodeType.Success, result + strMsg.ToString());
        }

        /// <summary>
        /// 库存处理
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        //[Authorization(Power = "PRIV_ERP_INVTRANSACTIONS")]
        [Authorization(Power = "PRIV_ERP_ALL")]
        public IActionResult InvTransAcitonsTOERP([FromBody] InvTranActionsRequestDto stringjson)
        {
            StringBuilder strMsg = new StringBuilder();
            //string result = string.Empty;
            InvTranActionsResponseDto result = new InvTranActionsResponseDto();
            string statuscode = string.Empty;
            try
            {
                result = _ToERP.InvTransActionsTOERP(stringjson, strMsg);
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            return toResponse<InvTranActionsResponseDto>(StatusCodeType.Success, strMsg.ToString(), result);
            //return toResponse(StatusCodeType.Success, result + strMsg.ToString());
        }



        ///// <summary>
        ///// 自动获取MES投料数据同步接口
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost]
        //[Authorization(Power = "PRIV_ERP_ALL")]
        //public IActionResult GetMESTLToERP([FromBody] string strjson)
        //{
        //    StringBuilder strMsg = new StringBuilder();
        //    List<MEStlDto> obj = new List<MEStlDto>();
        //    ERPTlResponseDto result = new ERPTlResponseDto();
        //    string statuscode = string.Empty;
        //    try
        //    {
        //        result = _ToERP.GetMESTLToERP(strjson, strMsg);
        //        if (string.IsNullOrEmpty(strMsg.ToString()))
        //        {
        //            statuscode = "S";
        //        }
        //        else
        //        {
        //            statuscode = "E";
        //        }

        //    }
        //    catch (Exception er )
        //    {
        //        strMsg.Append(er.Message.ToString());
        //    }
        //    //[20211104 shb update] 返回方式调整 start
        //    return toResponse<ERPTlResponseDto>(StatusCodeType.Success, strMsg.ToString(),result); 
        //}

        ///// <summary>
        ///// 不成功数据，循环投料
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost]
        //[Authorization(Power = "PRIV_ERP_ALL")]
        //public IActionResult GetTestClob([FromBody] string strjson)
        //{
        //    StringBuilder strMsg = new StringBuilder();
        //    //List<MEStlDto> obj = new List<MEStlDto>();
        //    //ERPTlResponseDto result = new ERPTlResponseDto();
        //    //string statuscode = string.Empty;
        //    try
        //    {
        //        //result = _ToERP.GetTestClob(strjson, strMsg);
        //        //if (string.IsNullOrEmpty(strMsg.ToString()))
        //        //{
        //        //    statuscode = "S";
        //        //}
        //        //else
        //        //{
        //        //    statuscode = "E";
        //        //}
        //        strMsg.Append("已关闭此接口！");
        //    }
        //    catch (Exception er)
        //    {
        //        strMsg.Append(er.Message.ToString());
        //    }
        //    //[20211104 shb update] 返回方式调整 start
        //    return toResponse(StatusCodeType.Success, strMsg.ToString()); ;
        //}

        ///// <summary>
        ///// 测试用
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost]
        ////[Authorization(Power = "PRIV_ERP_ALL")]
        //public IActionResult RecieveOraData([FromBody] string strjson)
        //{
        //    StringBuilder strMsg = new StringBuilder();

        //    try
        //    {
        //        //_logger.LogTrace(strjson);
        //        Meiam.System.Common.Helpers.LogHelpers.WriteLogTXT(strjson);
        //    }
        //    catch (Exception er)
        //    {
        //        strMsg.Append(er.Message.ToString());
        //    }
        //    //[20211104 shb update] 返回方式调整 start
        //    return toResponse(StatusCodeType.Success, strMsg.ToString()); ;
        //}


    }
}
